<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit=!isEdit"
      >{{isEdit?"完成":"编辑"}}</van-button>
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item" v-for="(channel , index) in myChannel" :key="index" @click="myChannelsClick(channel,index)"
      >
        <van-icon v-show="isEdit && !fixedChannel.includes(index)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span
          class="text" :class="{active:index===active}"
          slot="text"
        >{{channel.name}}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item v-for="(channel,index) in recommentChannels" :key="index"
        class="grid-item"
        icon="plus"
        :text="channel.name"
        @click="addChannel(channel)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import { getAllChannels } from '@/api/channel'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannel: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      AllChannels: [],
      isEdit: false,
      fixedChannel: [0] //  固定推荐频道,不可删除
    }
  },
  computed: {
    recommentChannels () {
      // let arr = [];  //  存储推荐频道
      // //  遍历所有的频道
      // this.AllChannels.forEach(channel=>{
      //   //  ret: true 为用户频道 , false 为推荐频道
      //   let ret = this.myChannel.find(myChannel=>{
      //     return myChannel.id === channel.id
      //   })
      //   if(!ret) {
      //     arr.push(channel)
      //   }
      // })
      // return arr

      //  高级用法
      return this.AllChannels.filter(channel => {
        return !this.myChannel.find(myChannel => {
          return myChannel.id === channel.id
        })
      })
    }
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.AllChannels = data.data.channels
        console.log(this.AllChannels)
      } catch (error) {
        this.$toast('获取所有的频道失败!')
      }
    },
    addChannel (channel) {
      this.myChannel.push(channel)
    },
    myChannelsClick (channel, index) {
      if (this.isEdit) {
        if (this.fixedChannel.includes(channel.id)) {
          return
        }
        if (index < this.active) {
          this.$emit('changeActive', this.active - 1, true)
        }
        this.myChannel.splice(index, 1)
      } else {
        this.$emit('changeActive', index, false)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text, .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
